package com.nt.train.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
 * https://blog.csdn.net/wangzhongshun/article/details/123973919
 * http://www.javashuo.com/article/p-aisyogfx-nv.html
 * 读取resource下的.properties文件，将文件中的内容封装到map中，注入到bean中方便依赖注入
 * @author hlf
 * @version 1.0
 * @date 2022/8/12 17:13
 */
@Configuration
public class PropertiesClassLoader {

    private Logger logger = LoggerFactory.getLogger(PropertiesClassLoader.class);

    private Map<String, Object> versionProperties = new HashMap<>(16);

    private void init(String name) {
        try {
            Properties properties = new Properties();
            InputStream in = PropertiesClassLoader.class.getClassLoader().getResourceAsStream(name + ".properties");
            properties.load(in);
            logger.info("加载{}.properties参数", name);
            for (String keyName : properties.stringPropertyNames()) {
                String value = properties.getProperty(keyName);
                if ("version".equals(name)) {
                    versionProperties.put(keyName, value);
                }
                logger.info("{}.properties---------key:{},value:{}", name, keyName, value);
            }
            logger.info("{}.properties参数加载完毕", name);
        } catch (IOException ignored) {

        }

    }

//    @Bean(name = "versionProperties")
//    public Map<String, Object> commonMap() {
//        init("version");
//        return versionProperties;
//    }
}